clear
set more off
frames reset
cap log close
log using $logdir/analysis/nrp_table.log, replace

// author: Lucas

frame create results strL(var) double(uncond cond)


use $nrpdir/nrp_raw, clear
merge 1:1 prim_tin using $nrpdir/w2, nogen
qui replace wgs_w2 = 0 if missing(wgs_w2)

*****************************
*		Mismatch rates		*
*****************************
foreach var in wgs interest dividends sched1 ira_pens ///
	capgain agi ti eitc ctc {

	if inlist("`var'","eitc","ctc") {
		local tol = 100
	}
	else {
		local tol = 500
	}


	
	tempvar uncond cond
	// unconditional
	qui gen `uncond' = abs(`var'_pre - `var'_post) > `tol'
	// conditional
	qui gen `cond' = abs(`var'_pre - `var'_post) > `tol' if `var'_pre ~= 0
	
	qui sum `uncond' [aw=nrp_wgt]
	local m_uncond = r(mean)
	
	qui sum `cond' [aw=nrp_wgt]
	local m_cond = r(mean)
	
	frame post results ("`var'") (`m_uncond') (`m_cond')
}


*****************************
*		W-2 cross-tab		*
*****************************
gen w2_mismatch = abs(wgs_pre - wgs_w2) > 500
gen audit_corr = abs(wgs_pre - wgs_post) > 500
qui keep if max(wgs_pre,wgs_w2,wgs_post) > 0

// if you are a w-2 mismatch and have an audit correction, what share have corrected wages within \$100 of W-2?
gen post_near10 = abs(wgs_post - wgs_w2) < 10
gen post_near500 = abs(wgs_post - wgs_w2) < 500

sum post_near* if audit_corr & w2_mismatch


collapse (sum) nrp_wgt, by(w2_mismatch audit_corr) fast
qui sum nrp_wgt
qui replace nrp_wgt = nrp_wgt/r(sum)
rename nrp_wgt f

reshape wide f, i(w2_mismatch) j(audit_corr)
list


// list the mismatch rates as well
frame change results
list


	
	

cap log close
